package com.yjps.system.controller;

import com.alibaba.fastjson.JSON;
import com.yjps.common.message.Message;
import com.yjps.common.page.Pager;
import com.yjps.common.util.CommonUtil;
import com.yjps.system.model.Complaint;
import com.yjps.system.model.User;
import com.yjps.system.service.ComplaintService;
import com.yjps.system.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 投诉控制类
 *     |--后台接口
 *           |--/system/complaint/list  获取投诉分页列表
 *           |--/system/complaint/accept  系统管理员受理投诉
 *           |--/system/complaint/getComplaint  获得指定id的投诉的json数据
 *    |--前台接口
 *           |--/company/complaint/edit.do  前台用户添加投诉请求
 *
 * Created by jessy on 2015/8/25.
 */
@Controller
public class ComplaintController {

    Logger log = LoggerFactory.getLogger(ComplaintController.class);
    @Autowired
    ComplaintService complaintService;
    @Autowired
    UserService userService;

    /**
     * 获取投诉分页列表
     * @param acceptResult  选择参数 已受理/未受理
     * @param model
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/system/complaint/list",method = RequestMethod.GET)
    public String list(String acceptResult,Model model,HttpServletRequest request,HttpServletResponse response) throws Exception {
        Pager<Complaint> pager = complaintService.getComplaintByPagerWithKey(new Pager<Complaint>(request, response), acceptResult);
        model.addAttribute(pager);
        return "system/complaint/list";
    }

    /**
     * 用户投诉请求页面
     * @return
     */
    @RequestMapping(value = "/company/complaint/edit",method = RequestMethod.GET)
    public String edit(){
        return "company/complaint/edit";
    }

    /**
     * 添加受理
     * @param complaint
     * @param request
     * @return {"success":true,"msg":"投诉编辑XX！","data":{"acceptResult":0,"acceptTime":"","complaintReason":"","complaintState":1,"complaintTime":"2015-08-27","id":0,"userId":1,"username":"","valid":1}
    }
     */
    @ResponseBody
    @RequestMapping(value = "/company/complaint/edit.do",method = RequestMethod.POST)
    public String edit(@RequestParam(value = "id",required = true)String userId,Complaint complaint,HttpServletRequest request){
//        User user = (User) request.getSession().getAttribute("loginUser");
        complaint.setUserId(Integer.parseInt(userId));
        complaint.setComplaintState(1);
        complaint.setComplaintTime(CommonUtil.dataToString(new Date()));
        complaint.setAcceptResult(0);
        complaint.setIsValid(1);
        int id = complaintService.addComplaint(complaint);
        /*model.addAttribute("complaint",complaint);*/
        Message message = null;
        if(id>0) {
            message = new Message(true, "投诉编辑成功！", complaint);
        }else{
            message = new Message(false, "投诉编辑失败！");

        }
        return message.toJson();
    }

    /**
     * 受理投诉
     * @param id
     * @return
     */
    @RequestMapping(value = "/system/complaint/accept",method =RequestMethod.GET)
    public String accept(@RequestParam(value = "id",required = true)String id){
        System.out.println("handler.......");
        Complaint complaint = complaintService.getComplaint(Integer.parseInt(id));
        complaint.setAcceptResult(1);
        complaint.setAcceptTime(CommonUtil.dataToString(new Date()));
        complaintService.updateComplaint(complaint);
        return "redirect:/system/complaint/list";
    }

    /**
     * 获得指定id的投诉的json数据
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/system/complaint/getComplaint",method = RequestMethod.GET)
    public String getComplaint(@RequestParam(value = "id",required = true)String id){
        Complaint c = complaintService.getComplaint(Integer.parseInt(id));
        User u = userService.findUser(c.getUserId());
        c.setUsername(u.getUsername());
        String jsonStr = JSON.toJSONString(c);
        return jsonStr;
    }

    @RequestMapping(value = "/system/complaint/delete",method = RequestMethod.GET)
    public String delete(@RequestParam(value = "id",required = true)String id){
        log.debug("delete user id " + id);
        complaintService.deleteComlaint(Integer.parseInt(id));
        return "redirect:/system/complaint/list";
    }
}
